package cs2340.droidceptiontodo;

import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

/**
 * Class handles list of todo items
 * @author Droidception
 *
 */
public class ToDoItemsActivity extends ListActivity implements OnClickListener {
	private Button addItem, filter, logout, showMap;
	private List<ToDoItem> items;
	private ToDoListAdapter adapter;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate (Bundle icicle) {
		super.onCreate(icicle);
		
		getItems();
		setContentView(R.layout.todo_items);
		
		// Adds ToDoItems into the view
		adapter = new ToDoListAdapter(this, items);
		setListAdapter(adapter);

				  
		addItem = (Button) findViewById(R.id.addItem);
		addItem.setOnClickListener(this);
		
		logout = (Button) findViewById(R.id.logout);
		logout.setOnClickListener(this);
		
		filter = (Button) findViewById(R.id.filter);
		filter.setOnClickListener(this);
		
		//viewItem = (Button) adapter.g
		
		showMap = (Button) findViewById(R.id.mapButton);
		if(showMap != null){
			Log.d("debug", "This button was found");
		}
		else
			Log.d("debug", "Map button wasn't found");
		showMap.setOnClickListener(this);
	}
	
	/**
	 * Method calls list of todoitems and creates ToDoListAdaptor instance 
	 * to display this list
	 * @param icicle
	 */
	public void OnRestoreInstanceState(Bundle icicle) {
		super.onRestoreInstanceState(icicle);
		getItems();
		adapter = new ToDoListAdapter(this, items);
		setListAdapter(adapter);
	}
	
	/**
	 * Calls appropriate activity when addItem, logout, or filter buttons pressed
	 */
	@Override
	public void onClick(View v) {
		if (v.getId() == (addItem.getId())){
			Intent i = new Intent();
    		i.setClassName("cs2340.droidceptiontodo", "cs2340.droidceptiontodo.AddToDoItemActivity");
    		startActivity(i);
		}
		else if (v.getId() == (logout.getId())) {
			Files.logoutUser();
			Intent i = new Intent();
    		i.setClassName("cs2340.droidceptiontodo", "cs2340.droidceptiontodo.LoginActivity");
    		startActivity(i);
		}
		else if(v.getId() == (filter.getId())){
			Intent i = new Intent();
			i.setClassName("cs2340.droidceptiontodo", "cs2340.droidceptiontodo.FilterActivity");
			startActivityForResult(i, 0);
		}
		else if(v.getId() == (showMap.getId())){
			Intent i = new Intent();
			i.setClassName("cs2340.droidceptiontodo", "cs2340.droidceptiontodo.MapsActivity");
			startActivity(i);
		}
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
        Object o = this.getListAdapter().getItem(position);
        String keyword = o.toString();
        Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
                .show();

    }
	@Override
	/**
	 * Called when user exits list of to-do items activity
	 */
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		getItems();
		adapter = new ToDoListAdapter(this, items);
		setListAdapter(adapter);
	}
	/**
	 * Gets list of to-do items from Database
	 */
	private void getItems() {
		Log.d("DEBUG", "Getting items");
		//creates a new database (which in reality, just pulls up the old one)
		Database settingsManager = Database.getInstance(this);
		//opens the database
		settingsManager.open();
		String filterByCategory = settingsManager.getSetting("filterByCategory");
		String filterByChecked = settingsManager.getSetting("filterByChecked");
		String sortByDate = settingsManager.getSetting("sortByDate");
		settingsManager.close();
		String sortBy = null;
		
		Log.d("DEBUG", filterByCategory + filterByChecked + sortByDate);
		
		if(sortByDate!=null && !sortByDate.equals("null")) {
			sortBy = "dueDate";
		}
		
		items = new ArrayList<ToDoItem>();
		try{
			//Database itemGetter = new Database(this);
			Database itemGetter = Database.getInstance(this);
			itemGetter.open();
			items = itemGetter.getFilteredItems(itemGetter.getUser().getID(), filterByCategory, filterByChecked, sortBy, sortByDate);
			itemGetter.close();
		}
		catch(Exception e){
			Log.d("DEBUG", e.toString());
		}
	}

	@Override
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		MenuInflater blowup = getMenuInflater();
		blowup.inflate(R.menu.listmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
			case R.id.delete:
				Database delItems = Database.getInstance(this);
				delItems.open();
				delItems.deleteChecked();
				delItems.close();
				try{
					startActivity(getIntent()); 
					finish();
				}
				catch(Exception e){
					Log.d("Debug", e.toString());
				}
				break;
		}
		return false;
	}

	
}